package com.alatus.web;

import com.alatus.constant.Constants;
import com.alatus.model.TClue;
import com.alatus.query.ClueQuery;
import com.alatus.result.Result;
import com.alatus.service.ClueService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class ClueController {
    @Resource
    private ClueService clueService;

    @PreAuthorize(value = "hasAuthority('clue:list')")
    @GetMapping(value = "/api/clues")
    public Result cluePage(@RequestParam(value = Constants.CURRENT,required = false)Integer current){
        if(current == null){
            current = 1;
        }
        PageInfo<TClue> pageInfo = clueService.getClueByPage(current);
        return Result.OK(pageInfo);
    }

    @PreAuthorize(value = "hasAuthority('clue:import')")
    @PostMapping(value = "/api/clue/importExcel")
    public Result importExcel(MultipartFile file, @RequestHeader(value = Constants.TOKEN_NAME)String token) throws IOException {
//        接收到的文件名字需要和前端提交的名字相同,否则无法接收
        clueService.importExcel(file.getInputStream(),token);
        return Result.OK();
    }

    @PreAuthorize(value = "hasAuthority('clue:delete')")
    @DeleteMapping(value = "/api/clue/delete/{id}")
    public Result clueDel(@PathVariable(value = Constants.ID)Integer id){
        Integer result = clueService.deleteClue(id);
        return result >= 1 ? Result.OK() : Result.FAIL();
    }

    @PreAuthorize(value = "hasAuthority('clue:delete')")
    @DeleteMapping(value = "/api/clue/batchDel")
    public Result batchDelClue(@RequestParam(value = Constants.IDS)String ids,@RequestHeader(value = Constants.TOKEN_NAME)String token){
        List<String> idsList = Arrays.asList(ids.split(","));
        int result = clueService.delClueByIds(idsList);
        return result >= idsList.size() ? Result.OK() : Result.FAIL();
    }

    @PreAuthorize(value = "hasAuthority('clue:view')")
    @GetMapping(value = "/api/clue/phone/{phone}")
    public Result checkPhone(@PathVariable(value = Constants.PHONE)String phone){
        Boolean check = clueService.checkPhone(phone);
        return check ? Result.OK() : Result.FAIL();
    }

    @PreAuthorize(value = "hasAuthority('clue:add')")
    @PostMapping(value = "/api/clue/add")
    public Result addClue(@RequestHeader(value = Constants.TOKEN_NAME)String token, ClueQuery clueQuery){
        clueQuery.setToken(token);
        int save = clueService.saveClue(clueQuery);
        return save >= 1 ? Result.OK() : Result.FAIL();
    }

    @PreAuthorize(value = "hasAuthority('clue:view')")
    @GetMapping(value = "/api/clue/{id}")
    public Result getClue(@PathVariable(value = Constants.ID)Integer id){
        TClue tClue = clueService.getClueById(id);
        return Result.OK(tClue);
    }

    @PreAuthorize(value = "hasAuthority('clue:edit')")
    @PutMapping(value = "/api/clue/edit")
    public Result editClue(@RequestHeader(value = Constants.TOKEN_NAME)String token, ClueQuery clueQuery){
        clueQuery.setToken(token);
        int update = clueService.editClue(clueQuery);
        return update >= 1 ? Result.OK() : Result.FAIL();
    }
}
